<?php

class Bank {
    const DB_HOST = "localhost";
    const DB_USER = "root";
    const DB_PASS = "";
    const DB_NAME = "bank";
    private $_db;

    static private $_instance;

    private function __construct() {
        $this->_db = new mysqli(self::DB_HOST,self::DB_USER,self::DB_PASS,self::DB_NAME);
    }
    private function __clone() {}

    static function getInstance() {
        if (!self::$_instance instanceof self)
            self::$_instance = new Bank();
        return self::$_instance;
    }

    function putMoney($sum, $order) {
        $balance = $this->getBalance($order);
        $balance += $sum;
        $query = "UPDATE orders SET balance = {$balance}
                  WHERE number = {$order}";

        if (!$this->_db->query($query)) {
            return FALSE;
        }
        $logQuery = "INSERT INTO transactions (order_id,type,bin)
                     VALUES (
                        (SELECT id FROM orders WHERE number = {$order}),
                        'пополнение счета',
                        1
                     )";
        $this->_db->query($logQuery);
        return TRUE;
    }

    function getMoney($sum, $order) {
        $balance = $this->getBalance($order);
        if ($balance >= $sum) {
            $balance -= $sum;
            $query = "UPDATE orders SET balance = {$balance}
                  WHERE number = {$order}";

            if (!$this->_db->query($query)) {
                return FALSE;
            }
            $logQuery = "INSERT INTO transactions (order_id,type,bin)
                     VALUES (
                        (SELECT id FROM orders WHERE number = {$order}),
                        'снятие со счета',
                        1
                     )";
            $this->_db->query($logQuery);
            return TRUE;
        }
    }

    function getBalance($order) {
        $query = "SELECT o.balance FROM orders AS o, client AS c
                  WHERE o.id = c.order_id AND o.number = {$order}";
        $result = $this->_db->query($query);

        if (!$result) return FALSE;
        $row = $result->fetch_assoc();

        return (int) $row['balance'];
    }

    function getClientByInn($inn) {
        $query = "SELECT c.name,c.phone,o.balance,o.number FROM client AS c, orders AS o
                  WHERE o.id = c.order_id AND c.inn = {$inn}";

        $result = $this->_db->query($query);

        if (!$result) return FALSE;

        $row = $result->fetch_assoc();
        return new Client($row['name'],
                          $inn,
                          $row['phone'],
                          $row['number']);
    }

    function addClient(Client $client) {
        $name = $client->name;
        $inn = $client->inn;
        $phone = $client->phone;
        $nOrder = $client->order;

        $query_n = "INSERT INTO orders(balance,number) VALUES (0, {$nOrder})";
        $this->_db->query($query_n);

        $lastId = $this->_db->insert_id;

        $query = "INSERT INTO client(name,inn,order_id,phone)
                  VALUES ('{$name}','{$inn}',$lastId,'{$phone}')";
        if (!$this->_db->query($query)) {
            return FALSE;
        }
        return TRUE;
    }
}